package com.CampaignManager.client;
import com.google.gwt.maps.client.MapPane;
import com.google.gwt.maps.client.MapPaneType;
import com.google.gwt.maps.client.MapWidget;
import com.google.gwt.maps.client.geom.LatLng;
import com.google.gwt.maps.client.geom.Point;
import com.google.gwt.maps.client.overlay.Overlay;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.SimplePanel;

public class TextOverlay extends Overlay {

    private LatLng latLng;
    private final SimplePanel textPanel;
    private MapWidget parentMap;    
    private MapPane pane;    
    private HTML textWidget = null;

    public TextOverlay(LatLng latLng, String text) {
        
    	this.latLng = latLng;
        
        textWidget = new HTML(text);
        
        textPanel = new SimplePanel();
        textPanel.add(textWidget);
    }

    @Override
    protected final void initialize(MapWidget map) {
        
    	parentMap = map;
        
        pane = map.getPane(MapPaneType.MARKER_PANE);
        pane.add(textPanel);
        
        Point locationPoint = parentMap.convertLatLngToDivPixel(getLatLng());        
        pane.setWidgetPosition(textPanel, locationPoint.getX(), locationPoint.getY());
    }

    @Override
    protected final Overlay copy() {
        return new TextOverlay(getLatLng(), getText());
    }
    
    @Override
    protected final void redraw(boolean force) {

    	Point locationPoint = parentMap.convertLatLngToDivPixel(getLatLng());        
        pane.setWidgetPosition(textPanel, locationPoint.getX(), locationPoint.getY());
    }

    @Override
    protected final void remove() {
        textPanel.removeFromParent();
    }

	public String getText() {
		return textWidget.getHTML();
	}

	public void setText(String text) {
		this.textWidget.setHTML("<b>"+text+"</b>");
	}

	public LatLng getLatLng() {
		return latLng;
	}
	public void setLatLng(LatLng latLng) {
		this.latLng = latLng;  
	}
}